<?php

set_time_limit ( 180 );

require_once 'Common/Core.Common.php';

set_include_path ( dirname ( __FILE__ ) . '/lib/' );

require_once 'OpenSDK/Tencent/Weibo.php';

$appKey = '';
$appSecret = '';

OpenSDK_Tencent_Weibo::init ( $appKey, $appSecret );

session_start ();

header ( 'Content-Type: text/html; charset=utf-8' );

$exit = false;

if (isset ( $_GET ['exit'] )) {
	
	unset ( $_SESSION [OpenSDK_Tencent_Weibo::OAUTH_TOKEN] );
	unset ( $_SESSION [OpenSDK_Tencent_Weibo::ACCESS_TOKEN] );
	unset ( $_SESSION [OpenSDK_Tencent_Weibo::OAUTH_TOKEN_SECRET] );
	
	Go_OAuth ();

} else if (isset ( $_SESSION [OpenSDK_Tencent_Weibo::ACCESS_TOKEN] ) && isset ( $_SESSION [OpenSDK_Tencent_Weibo::OAUTH_TOKEN_SECRET] )) {
	
	ok ();
	
	$exit = true;

} else if (isset ( $_GET ['oauth_token'] ) && isset ( $_GET ['oauth_verifier'] )) {
	
	if (OpenSDK_Tencent_Weibo::getAccessToken ( $_GET ['oauth_verifier'] )) {
		
		ok ();
	
	} else {
		
		no ();
	
	}
	
	$exit = true;

} else if (isset ( $_GET ['go_oauth'] )) {
	
	$callback = 'http://' . $_SERVER ['HTTP_HOST'] . $_SERVER ['PHP_SELF'];
	$request_token = OpenSDK_Tencent_Weibo::getRequestToken ( $callback );
	$url = OpenSDK_Tencent_Weibo::getAuthorizeURL ( $request_token );
	header ( 'Location: ' . $url );

} else {
	
	Go_OAuth ();

}

if ($exit) {
	
	echo '<a href="?exit">Exit</a>';

}

function Go_OAuth() {
	
	echo '<a href="?go_oauth">Go OAuth</a>';
}

function ok() {
	
	$params = array ('pageflag' => 0, 'pagetime' => 0, 'reqnum' => 10, 'lastid' => 0 );
	$broadcast_timeline = OpenSDK_Tencent_Weibo::call ( 'statuses/broadcast_timeline', $params );
	
	foreach ( $broadcast_timeline ['data'] ['info'] as $tObj ) {
		
		$objTdel = OpenSDK_Tencent_Weibo::call ( 't/del', array ('id' => $tObj ['id'] ), 'POST' );
		dump ( $objTdel );
		sleep ( 2 );
	
	}
	
	for($i = 0; $i < 10; $i ++) {
		
		$objTadd = OpenSDK_Tencent_Weibo::call ( 't/add', array ('content' => time (), 'clientip' => '127.0.0.1' ), 'POST' );
		dump ( $objTadd );
		sleep ( 5 );
	
	}
	
	$other = OpenSDK_Tencent_Weibo::call ( 'other/kownperson' );
	
	for($i = 0; $i < 5; $i ++) {
		
		$oObj = $other ['data'] [$i];
		
		$objFadd = OpenSDK_Tencent_Weibo::call ( 'friends/add', array ('name' => $oObj ['name'] ), 'POST' );
		dump ( $objFadd );
		
		$objFdel = OpenSDK_Tencent_Weibo::call ( 'friends/del', array ('name' => $oObj ['name'] ), 'POST' );
		dump ( $objFdel );
	
	}

}

function no() {
	
	echo 'get Access Token failed';

}
